import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:rjpb/page/login/controllers/login_controllers.dart';
import 'package:jovial_svg/jovial_svg.dart'; //svg

class LoginPage extends StatelessWidget {
  static const String routeName = '/login'; // 添加静态路由名称
  final LoginController controller = Get.put(LoginController()); // 依赖注入

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          color: Color(0xFFCD7FEF),
          image: DecorationImage(
            image: AssetImage('assets/bg.png'),
            fit: BoxFit.fill,
          ),
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Image.asset('assets/rjlogo.png'),
              // ScalableImageWidget.fromSISource(
              //   si: ScalableImageSource.fromSvgHttpUrl(Uri.parse(
              //       'https://gitee.com/chen-jiahui_000909/svgtu/raw/master/Logo.svg')),
              // ),
              SizedBox(
                width: 260, // 设置您需要的宽度
                height: 160, // 设置您需要的高度
                child: ScalableImageWidget.fromSISource(
                  si: ScalableImageSource.fromSvgHttpUrl(Uri.parse(
                      'https://gitee.com/chen-jiahui_000909/svgtu/raw/master/renjilogo.svg')),
                ),
              ),

              // SizedBox(height: 60),
              Text(
                '仁济医院(南院)智慧消防',
                style: TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),
              SizedBox(height: 20),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20),
                child: Container(
                  width: 800,
                  alignment: Alignment.centerLeft,
                  child: Text(
                    '欢迎登录！',
                    style: TextStyle(
                      fontSize: 20,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
              SizedBox(height: 20),
              _buildTextField(
                onChanged: (value) => controller.username.value = value,
                hintText: '请输入用户名',
              ),
              SizedBox(height: 30),
              _buildTextField(
                onChanged: (value) => controller.password.value = value,
                hintText: '请输入密码',
                obscureText: true,
              ),
              SizedBox(height: 30),
              // Obx(() => _buildLoginButton(controller.isLoading.value)),
              Obx(() => _buildLoginButton(context, controller.isLoading.value)),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildTextField({
    required Function(String) onChanged,
    required String hintText,
    bool obscureText = false,
  }) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: SizedBox(
        width: 800,
        child: TextField(
          onChanged: onChanged,
          obscureText: obscureText,
          decoration: InputDecoration(
            hintText: hintText,
            hintStyle: TextStyle(color: Colors.white),
            filled: false,
            fillColor: Colors.transparent,
            focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.white, width: 1.0),
            ),
            enabledBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.white, width: 1.0),
            ),
          ),
          style: TextStyle(color: Colors.white),
          cursorColor: Colors.white,
        ),
      ),
    );
  }

  Widget _buildLoginButton(BuildContext context, bool isLoading) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: Container(
        width: 800,
        height: 40,
        child: ElevatedButton(
          // onPressed: isLoading ? null : () => controller.handleLogin(),
          onPressed: isLoading
              ? null
              : () => controller.handleLogin(context), // 传递 context
          child: isLoading
              ? CircularProgressIndicator(color: Colors.white)
              : Text('登录'),
        ),
      ),
    );
  }
}
